﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;

namespace WindowsGame1
{
    class Robot
    {
        Sprite left, right, up, down;
        Sprite currentSprite;

        public Vector2 position;
        public Vector2 speed;
        private Vector2 actualScreenSize;
        public Robot()
        {
            left = new Sprite("left", new Point(14, 17), new Point(3, 1), Point.Zero, 200);
            right = new Sprite("right", new Point(14, 17), new Point(3, 1), Point.Zero, 200);
            up = new Sprite("up", new Point(14, 17), new Point(3, 1), Point.Zero, 200);
            down = new Sprite("down", new Point(14, 17), new Point(3, 1), Point.Zero, 200);
            currentSprite = left;
            actualScreenSize = new Vector2(Game1.ScreenWidth - 14, Game1.ScreenHeight - 17);
            position = new Vector2((float)MathTools.Instance.GetRandomNumber(0, (int)actualScreenSize.X) / 10, (float)MathTools.Instance.GetRandomNumber(0, (int)actualScreenSize.Y) / 10);
            speed = new Vector2((float)MathTools.Instance.GetRandomNumber() / 10, (float)MathTools.Instance.GetRandomNumber() / 10);
            ChangeDirection(speed.X > 0 ? "right" : "left");
            ChangeDirection(speed.Y > 0 ? "down" : "up");
        }


        private void correctDirection(string direction)
        {
            switch (direction)
            {
                case "left":
                case "right":
                    {
                        if (Math.Abs(speed.X) < Math.Abs(speed.Y))
                        {
                            if (speed.Y > 0)
                                currentSprite = down;
                            else {
                                currentSprite = up;
                            }
                        }
                    }
                    break;
                case "up":
                case "down":
                    {
                        if (Math.Abs(speed.Y) < Math.Abs(speed.X))
                        {
                            if (speed.X > 0)
                                currentSprite = right;
                            else
                            {
                                currentSprite = left;
                            }
                        }
                    }
                    break;
                default:
                    break;
            }
        }

        public void ChangeDirection(string direction)
        {
            switch (direction)
            { 
                case "left":
                    currentSprite = left;
                    break;
                case "right":
                    currentSprite = right;
                    break;
                case "up":
                    currentSprite = up;
                    break;
                case "down":
                    currentSprite = down;
                    break;
                default:
                    break;
            }
            correctDirection(direction);
        }

        public void AddSprite(string spriteName)
        {
        
        }

        public void SetPosition(Vector2 position)
        {   
            this.position = position;
        }

        public virtual void Update(GameTime gameTime)
        {
            Vector2 oldPosition = this.position;
            position += speed;
            if (position.X > actualScreenSize.X || position.X < 0)
            {
                speed.X = -speed.X;
                ChangeDirection(speed.X > 0 ? "right" : "left");
            }
            if (position.Y > actualScreenSize.Y || position.Y < 0)
            {
                speed.Y = -speed.Y;
                ChangeDirection(speed.Y > 0 ? "down" : "up");
            }
            currentSprite.Update(gameTime);
        }

        public virtual void Draw(SpriteBatch spriteBacth)
        {
            currentSprite.Draw(spriteBacth, position);
        }
    }
}
